﻿
//#region jtoast

(function ($) {

    $.fn.jtoast = function (objPar) {
        objPar = $.extend({
            type: 'info',
            timeout: 10000
        }, objPar);
        if (objPar) {
            var $jtoast;
            if (objPar.link != undefined) {
                $jtoast = $('<span class="jtoast link ' + objPar.type + 'Type">' + objPar.link + '</span>').appendTo(this);
                delete objPar.link;
                $jtoast.data('jtoast', objPar);
            } else if (objPar.htmlLink != undefined) {
                $jtoast = $(objPar.htmlLink).appendTo(this).addClass('jtoast link');
                delete objPar.htmlLink;
                $jtoast.data('jtoast', objPar);
            } else if (objPar.body != undefined) {
                //#region remove old
                $('.jtoast:not(.link)').animate({
                    opacity: 0
                }, 150, function () {
                    $(this).remove();
                });
                //#endregion
                //#region append new
                $jtoast = $('<div class="jtoast"><div></div><h3 class="default"></h3><p class="default"></p></div>')
                        .appendTo($('html'))
                        .addClass(objPar.type),
                    $title = $jtoast.children('h3');
                if (objPar.title)
                    $title.html(objPar.title);
                else
                    $title.remove();
                $jtoast.children('p').html(objPar.body);
                //#endregion
                //#region animation new
                $jtoast.css({
                    bottom: -$jtoast.outerHeight(true) + 'px',
                    opacity: 1
                }).animate({
                    bottom: '0px'
                }, {
                    duration: 300
                });
                if (objPar.timeout != false) {
                    jQuery.fx.interval = 6;
                    var time = $jtoast.width();
                    $jtoast.children('div').animate({
                        width: time
                    }, objPar.timeout, 'linear', function () {
                        jQuery.fx.interval = 13;
                        $jtoast.click();
                    });
                    //                    $jtoast.children('div').css({
                    //                        'transition': 'all ' + objPar.timeout + 'ms',
                    //                        width: $jtoast.width() + 'px'
                    //                    });
                    //                    setTimeout(function () {
                    //                        $jtoast.click();
                    //                    }, objPar.timeout);
                }

                //#endregion
            }
        }
        return this;
    };

    $(document).on('click', '.jtoast.link', function () {
        $(this).jtoast($(this).data('jtoast'));
    }).on('click', '.jtoast:not(.link)', function () {
        var $this = $(this);
        $this.animate({
            bottom: -$this.outerHeight(true) + 'px'
        }, {
            duration: 350,
            complete: function () {
                $this.remove();
            }
        });
    });

})(jQuery);

//#endregion